﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace UrlChecker.Analysis
{
    public partial class ucKeyword : UserControl
    {
        #region UserControl

        public ucKeyword()
        {
            InitializeComponent();
            InitGroup();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.pbClose.Location = new Point(this.Width - this.pbClose.Width - 3, 3);
        }

        #endregion

        #region Method

        private void InitGroup()
        {
            DataTable dt = QT.SEO.Group.GetAll();
            this.cbbGroup.DisplayMember = "gname";
            this.cbbGroup.ValueMember = "guid";
            this.cbbGroup.DataSource = dt;
        }

        #endregion

        #region Event

        #region ComboBox

        private void cbbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = QT.SEO.URL.GetAll(this.cbbGroup.SelectedValue.ToString());
            this.cbbURL.DisplayMember = "url";
            this.cbbURL.ValueMember = "guid";
            this.cbbURL.DataSource = dt;
        }

        private void cbbURL_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbbKeyword.Items.Clear();
            DataTable dt = QT.SEO.URL.Get(this.cbbURL.SelectedValue.ToString());
            if (!string.IsNullOrEmpty(dt.Rows[0]["keywords"].ToString()))
            {
                List<string> keywords = new List<string>(dt.Rows[0]["keywords"].ToString().Split('|'));
                foreach (string keyword in keywords) this.cbbKeyword.Items.Add(keyword);
            }

            this.cbbKeyword.Items.Insert(0, "-全部-");
            this.cbbKeyword.SelectedIndex = 0;
        }

        #endregion

        #region Button

        private void btnCheck_Click(object sender, EventArgs e)
        {
            DataTable dt = null;
            if (this.cbbKeyword.SelectedIndex == 0) dt = QT.SEO.Keyword.Get(this.cbbURL.SelectedValue.ToString());
            else dt=QT.SEO.Keyword.Get(this.cbbURL.SelectedValue.ToString(),this.cbbKeyword.SelectedItem.ToString());

            this.dgvResult.AutoGenerateColumns = false;
            this.dgvResult.Columns[0].DataPropertyName = "Keyword";
            this.dgvResult.Columns[1].DataPropertyName = "SearchName";
            this.dgvResult.Columns[2].DataPropertyName = "rank";
            this.dgvResult.Columns[3].DataPropertyName = "snapdate";
            this.dgvResult.Columns[4].DataPropertyName = "checkdate";
            this.dgvResult.DataSource = dt;
            this.dgvResult.AutoResizeColumns();
        }

        #endregion         

        #region PictureBox

        private void pbClose_Click(object sender, EventArgs e)
        {
            ((Main)this.ParentForm).tsslMsg.Text = "";
            ((Main)this.ParentForm).tsslProgress.Text = "";
            this.Dispose();
        }

        private void pbClose_MouseHover(object sender, EventArgs e)
        {
            this.pbClose.Image = Properties.Resources.close02;
        }

        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            this.pbClose.Image = Properties.Resources.close01;
        }

        #endregion

        #endregion
    }
}
